from datetime import date
from typing import Union, List, Optional

from fastapi import FastAPI, Query
import uvicorn
from pydantic import BaseModel, field_validator

app = FastAPI()


class Addr(BaseModel):
    province: str
    city: str


class User(BaseModel):
    # name:str = Query(regex='^a')
    name: str
    age: int = Query(default=0, ge=0, lt=100)
    birth: Union[date, None] = None
    friends: List[int] = []
    description: Optional[str] = None
    address: Addr  # 嵌套类型

    @field_validator('name')
    def name_nust_alpha(cls, value):
        assert value.isalpha(), 'name must be alpha'
        return value


@app.post("/data")
async def data(user: User):
    print(user.name, user.age)
    return user


if __name__ == '__main__':
    uvicorn.run(app, host='127.0.0.1', port=8000)
